package cn.getech.data.metadata.service.impl;

import cn.getech.data.metadata.entity.CollectionTableChangeNotifyHistoryEntity;
import cn.getech.data.metadata.model.dto.CollectionTableChangeNotifyDto;
import cn.getech.data.metadata.model.dto.CollectionTableFieldDto;
import cn.getech.data.metadata.service.CollectionTableChangeNotifyHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.getech.data.intelligence.common.utils.PageUtils2;
import cn.getech.data.intelligence.common.utils.Query;
import cn.getech.data.intelligence.common.exception.RRException;

import cn.getech.data.metadata.mapper.CollectionTableChangeNotifyMapper;
import cn.getech.data.metadata.entity.CollectionTableChangeNotifyEntity;
import cn.getech.data.metadata.service.CollectionTableChangeNotifyService;


@Service("collectionTableChangeNotifyService")
public class CollectionTableChangeNotifyServiceImpl extends ServiceImpl<CollectionTableChangeNotifyMapper, CollectionTableChangeNotifyEntity> implements CollectionTableChangeNotifyService {
    @Autowired
    private CollectionTableChangeNotifyHistoryService collectionTableChangeNotifyHistoryService;

    @Override
    public PageUtils2<CollectionTableChangeNotifyEntity> queryPage(Map<String, Object> params) {
        IPage<CollectionTableChangeNotifyEntity> page = this.page(
                new Query<CollectionTableChangeNotifyEntity>().getPage(params),
                new QueryWrapper<CollectionTableChangeNotifyEntity>()
        );

        return new PageUtils2<CollectionTableChangeNotifyEntity>(page);
    }

    @Override
    public List<CollectionTableChangeNotifyDto> listChangeNotify() {
        return baseMapper.listChangeNotify();
    }

    @Override
    public List<CollectionTableFieldDto> listTableField(Long taskLogId, Integer tableId) {
        Map<String,Object> params = new HashMap<>();
        params.put("taskLogId", taskLogId);
        params.put("tableId", tableId);
        return baseMapper.listTableField(params);
    }

    @Override
    public void deleteAndAddHistory(Long tcnId) {
        CollectionTableChangeNotifyEntity changeNotifyEntity = this.getById(tcnId);
        if(changeNotifyEntity == null)
            throw new RRException("没有获取到记录");

        CollectionTableChangeNotifyHistoryEntity historyEntity = new CollectionTableChangeNotifyHistoryEntity();
        historyEntity.setTcnId(changeNotifyEntity.getTcnId());
        historyEntity.setTableId(changeNotifyEntity.getTableId());
        historyEntity.setTaskId(changeNotifyEntity.getTaskId());
        historyEntity.setTaskLogId(changeNotifyEntity.getTaskLogId());
        historyEntity.setCreateTime(changeNotifyEntity.getCreateTime());
        collectionTableChangeNotifyHistoryService.save(historyEntity);

        this.removeById(tcnId);
    }


}
